package com.frogs.ucenter.v1.request;

import javax.servlet.http.HttpServletRequest;

import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.exception.RequestIllegalException;
import com.frogs.ucenter.util.CpsConstant;

public class UserLoginByDynamicPasswordRequest extends Request {

	private String username;
	
	private String dynamicPassword;
	
	private String expandCode;
	
	private Integer applicablePlatformId;

	/**
	 * 登陆ip
	 */
	private String ip;
	
	public UserLoginByDynamicPasswordRequest() {
	}
	
	@Override
	public void parse(HttpServletRequest request) {
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		
		this.username = requestInfo.getParameter(ApiUrlParamEnum.username.getParamName(), "");
		this.dynamicPassword = requestInfo.getParameter(ApiUrlParamEnum.dynamicPassword.getParamName(), "");
		this.ip = requestInfo.getParameter(ApiUrlParamEnum.ip.getParamName(), requestInfo.getUserIp());
		this.expandCode = requestInfo.getParameter(ApiUrlParamEnum.expandCode.getParamName(), requestInfo.getCookieValue(CpsConstant.CPS_SOURCE_NAME,""));
		this.applicablePlatformId = requestInfo.getIntParameter(ApiUrlParamEnum.applicablePlatformId.getParamName(), ApplicablePlatformEnum.WEB.getId());
		if (StringUtil.isEmpty(username)) {
			throw new RequestIllegalException("账号名不为空");
		}
		if (StringUtil.isEmpty(dynamicPassword)) {
			throw new RequestIllegalException("动态密码不能为空");			
		}
		if ("127.0.0.1".equals(ip)) {
			this.ip = requestInfo.getUserIp();
		}
		if (StringUtil.isEmpty(ip)) {
			throw new RequestIllegalException("ip不能为空");
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getDynamicPassword() {
		return dynamicPassword;
	}

	public void setDynamicPassword(String dynamicPassword) {
		this.dynamicPassword = dynamicPassword;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getExpandCode() {
		return expandCode;
	}

	public void setExpandCode(String expandCode) {
		this.expandCode = expandCode;
	}

	public Integer getApplicablePlatformId() {
		return applicablePlatformId;
	}

	public void setApplicablePlatformId(Integer applicablePlatformId) {
		this.applicablePlatformId = applicablePlatformId;
	}

}
